/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConditionsView.java
 *
 * Created on Feb 3, 2010, 11:48:45 AM
 * Author: barisergun
 */
package org.testis.gui.mvc.view.testdesign;

// Java libraries
import org.testis.gui.mvc.controller.testdesign.ConditionsController;
import org.testis.gui.mvc.view.AbstractViewPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author barisergun
 */
public class ConditionsView extends javax.swing.JPanel implements ActionListener, AbstractViewPanel {

    private JPopupMenu jSelectionPopup;
    private JMenuItem jMenuItemEdit;
    private JMenuItem jMenuItemRemove;
    private static final String EDIT_CONDITION = "Edit Condition";
    private static final String REMOVE_CONDITION = "Remove Condition";
    private static final String NEW_CONDITION = "New Condition";

    /** Creates new form ConditionsView */
    public ConditionsView() {
        initComponents();

        jButtonNewCondition.setText(NEW_CONDITION);
        //Create the popup menu.
        jSelectionPopup = new JPopupMenu();
        jMenuItemEdit = new JMenuItem(EDIT_CONDITION);
        jMenuItemEdit.addActionListener(this);
        jSelectionPopup.add(jMenuItemEdit);
        jMenuItemRemove = new JMenuItem(REMOVE_CONDITION);
        jMenuItemRemove.addActionListener(this);
        jSelectionPopup.add(jMenuItemRemove);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListConditionsList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jButtonNewCondition = new javax.swing.JButton();

        jListConditionsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListConditionsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jListConditionsListMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jListConditionsListMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jListConditionsList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
        );

        jButtonNewCondition.setText("New Condition");
        jButtonNewCondition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewConditionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jButtonNewCondition)
                .addContainerGap(252, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButtonNewCondition)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Mouse events
    private void jListConditionsListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListConditionsListMouseReleased
        if (evt.getButton() == MouseEvent.BUTTON3) {

            if (evt.isPopupTrigger()
                    && jListConditionsList.getModel().getSize() != 0) {
                jSelectionPopup.show(evt.getComponent(),
                        evt.getX(), evt.getY());
            }
        }
    }//GEN-LAST:event_jListConditionsListMouseReleased

    private void jButtonNewConditionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewConditionActionPerformed
        conditionListActionPerformed(evt);
    }//GEN-LAST:event_jButtonNewConditionActionPerformed

    private void jListConditionsListMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListConditionsListMousePressed
        if (evt.getButton() == MouseEvent.BUTTON3) {
            // This code is to select the ListItem when BUTTON3 is pressed.
            // Default behaviour is BUTTON1 press for selection.
            int y = jListConditionsList.locationToIndex(evt.getPoint());
            if (y >= 0) {
                jListConditionsList.setSelectedIndex(
                        jListConditionsList.locationToIndex(evt.getPoint()));
            }
        } else if (evt.getButton() == MouseEvent.BUTTON1) {
            if (evt.getClickCount() == 2) {
                // calls the doClick event of Edit Item of popup menu.
                jMenuItemEdit.doClick();
            }
        }

    }//GEN-LAST:event_jListConditionsListMousePressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonNewCondition;
    private javax.swing.JList jListConditionsList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    // Menu Item events
    @Override
    public void actionPerformed(ActionEvent ae) {
        conditionListActionPerformed(ae);
    }

    //Common events
    public void conditionListActionPerformed(ActionEvent ae) {

        if (ae.getActionCommand().equals(EDIT_CONDITION)) {
            AtofCondListPair selectedObject =
                    (AtofCondListPair) jListConditionsList.getModel().getElementAt(
                    jListConditionsList.getSelectedIndex());

            ConditionsController.getInstance().operationEditItemFromList(
                    selectedObject.getObjectId(), selectedObject.getObjectValue());
        } else if (ae.getActionCommand().equals(REMOVE_CONDITION)) {
            AtofCondListPair selectedObject =
                    (AtofCondListPair) jListConditionsList.getModel().getElementAt(
                    jListConditionsList.getSelectedIndex());

            ConditionsController.getInstance().
                    operationRemoveItemFromList(selectedObject.getObjectId());

        } else if (ae.getActionCommand().equals(NEW_CONDITION)) {
            ConditionsController.getInstance().operationNewItemFromList();
        }
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(ConditionsController.CHANGE_CONDITIONS_LIST)) {
            LinkedHashMap<Integer, String> newModelData =
                    (LinkedHashMap<Integer, String>) evt.getNewValue();
            Vector<AtofCondListPair> vectorOfConditions = new Vector<AtofCondListPair>();

            Set<Entry<Integer, String>> ourSet = newModelData.entrySet();
            for (Iterator<Entry<Integer, String>> it = ourSet.iterator(); it.hasNext();) {
                Entry<Integer, String> condString = it.next();
                AtofCondListPair listItem = new AtofCondListPair(condString.getKey()
                        ,condString.getValue());

                vectorOfConditions.add(listItem);
            }
            jListConditionsList.setListData(vectorOfConditions);

        }

    }

    class AtofCondListPair extends Object{

        private Integer m_ObjectId;
        private String m_ObjectValue;

        public AtofCondListPair(Integer id, String value){
            m_ObjectId = id;
            m_ObjectValue = value;
        }

        public Integer getObjectId() {
            return m_ObjectId;
        }

        public void setObjectId(Integer objectId) {
            this.m_ObjectId = objectId;
        }

        public String getObjectValue() {
            return m_ObjectValue;
        }

        public void setObjectValue(String objectValue) {
            this.m_ObjectValue = objectValue;
        }

        @Override
        public String toString()
        {
            return m_ObjectValue;
        }
        
    }
}
